// 发送短信
var smsJson = {
	apikey:"966f88af7f6c20b51bb758ffa50c197c",
	text:"【比特币网站】欢迎使用陆家嘴比特币，您的验证码是000000， 本次验证码10分钟内有效",
	mobile:""
};

exports.sendsms = async function(req, res){
	var username = req.body.username || '';
	console.log(req.body)
	if(username == ''){
		res.json({status:403, msg:'手机号不能为空'}); //手机号为空
	}
	redisClient.get(username, function (err, code) {
		console.log(err, code)
		if(code){
			res.json({status:501, ''});
		}else{

		}
		    

			var SMS_CODE = moment(new Date()).format("mmssSS"); //验证码
			smsJson.mobile = username;
			smsJson.text = smsJson.text.replace("000000", SMS_CODE); // 验证码替换模板
			
			console.log(smsJson);

			request.post({url:SMS_URL, form: smsJson}, function(err,httpResponse,body){
				console.log(body)
				var object = JSON.parse(body);
				if(object["code"]==0){
					console.log("redissms", username, SMS_CODE)
					redisClient.set(username, SMS_CODE);
    				redisClient.expire(username, TIME_OUT*2); // 10分钟失败
    				redisClient.del(vid);
    				res.sendStatus(200);
				}else{
					 res.json({status:501, msg:object["msg"] });
				}
			});

	});
};
